﻿using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;

namespace PyramidShadow
{
    class Level 
    {
        // Keyboard states used to determine key presses
        private KeyboardState currentKeyboardState;
        private KeyboardState previousKeyboardState;

        // Gamepad states used to determine button presses
        private GamePadState currentGamePadState;
        private GamePadState previousGamePadState;

        public Map map;
        public Ghost ghost;
        public Body body;
        public Kollision kollision;

        // A movement speed for the player
        public byte playerMoveSpeed = 3;

        private Game1 game;
        public Game1 Game 
        {
            get { return game; }
        }  
        
        public Level(Game1 game,MapBuilder levelContent)
        {

            map = new Map(levelContent.MapMembers.map_tex_bg, levelContent.MapMembers.map_tex_cm, this, levelContent);
            body = new Body(levelContent.BodyMembers.body_tex, levelContent.BodyMembers.body_pos,this);
            ghost = new Ghost(levelContent.GhostMembers.ghost_tex, levelContent.GhostMembers.ghost_pos,this);
            kollision = new Kollision(this);
            this.game = game;
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime)
        {
            // Save the previous state of the keyboard and game pad so we can determinesingle key/button presses
            previousGamePadState = currentGamePadState;
            previousKeyboardState = currentKeyboardState;

            // Read the current state of the keyboard and gamepad and store it
            currentKeyboardState = Keyboard.GetState();
            currentGamePadState = GamePad.GetState(PlayerIndex.One);

            /**************************************************************************
             * The order is important so the kollison and reset methods work properly!*
             **************************************************************************/
            //Update the player
            ghost.Update(gameTime, currentKeyboardState);
            body.Update(gameTime, currentKeyboardState);
            //Update the map
            map.Update(gameTime,currentKeyboardState);
            
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            body.Draw(spriteBatch);
            ghost.Draw(spriteBatch);
            map.Draw(spriteBatch);
        }

        public void showVictoryScreen()
        {
            new VictoryScreen(this,game.SpriteBatch);
        }
    }
}
